let storage = window.sessionStorage

/**
 * 存储cookie
 * @param key
 * @param value
 */
let setItem = function (key, value) {
  if (typeof value === 'object') {
    value = JSON.stringify(value)
  }
  storage.setItem(key, value)
  return storage.getItem(key)
}

/**
 * 获取cookie
 * @param key
 * @returns {boolean}
 */
let getItem = function (key) {
  let temp = storage.getItem(key)
  if (temp === 'true') {
    return true
  } else if (temp === 'false') {
    return false
  }
  if (temp && (temp.substring(0, 1) === '{' || temp.substring(0, 1) === '[')) {
    return JSON.parse(temp)
  }
  return temp
}

/**
 * 删除cookie
 * @param key
 */
let removeItem = function (key) {
  let temp = storage.getItem(key)
  if (temp != null) {
    storage.removeItem(key)
  }
}

/**
 * 清除cookie
 */
let clear = function () {
  storage.clear()
}

export default {
  setItem,
  getItem,
  removeItem,
  clear
}
